package com.datang.pet.service.impl;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-06-24 09:09
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.ossUtils.DeleteFile;
import com.datang.pet.common.ossUtils.OssUtil;
import com.datang.pet.common.redis.RedisTool;
import com.datang.pet.common.utils.StringUtils;
import com.datang.pet.mapper.CityMapper;
import com.datang.pet.mapper.GivePetMapper;
import com.datang.pet.mapper.ResourceMapper;
import com.datang.pet.mapper.WxTemplateMapper;
import com.datang.pet.mapperExtend.GivePetMapperExtend;
import com.datang.pet.mapperExtend.ResourceMapperExtend;
import com.datang.pet.pojo.*;
import com.datang.pet.service.GivePetService;
import com.datang.pet.service.MessageService;
import com.datang.pet.vo.AddGivePetRequest;
import com.datang.pet.vo.GetGiveInfoResponse;
import com.datang.pet.vo.GetGivePetListByCityResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class GivePetServiceImpl implements GivePetService {

    @Autowired
    private GivePetMapper givePetMapper;

    @Autowired
    private WxTemplateMapper wxTemplateMapper;

    @Autowired
    private ResourceMapperExtend resourceMapperExtend;

    @Autowired
    private MessageService messageService;

    @Autowired
    private GivePetMapperExtend givePetMapperExtend;

    @Autowired
    private CityMapper cityMapper;

    @Autowired
    private RedisTool redisTool;

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private DeleteFile deleteFile;

    /*
     * @author 顶风少年
     * @date 2019/6/25 11:27
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param addGivePetRequest
     * @result void
     * @notify 添加送养
     * version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public void AddGivePet(AddGivePetRequest addGivePetRequest) throws Exception {

        //设置基本属性
        addGivePetRequest.setId(StringUtils.getId());
        addGivePetRequest.setCreateDate(new Date());
        addGivePetRequest.setCancel("0");
        addGivePetRequest.setResource(StringUtils.getId());
        GivePet givePet = new GivePet();
        //拷贝属性
        BeanUtils.copyProperties(addGivePetRequest, givePet);
        try {
            givePetMapper.insert(givePet);
            redisTool.remove("giveListByUserId" + addGivePetRequest.getUserId());

        } catch (Exception e) {
            throw new Exception("添加送养失败");
        }


        //图片id
        String resourceId = addGivePetRequest.getResource();
        for (Resource r : addGivePetRequest.getUrlList()) {
            r.setId(resourceId);
        }
        try {
            resourceMapperExtend.addResourceList(addGivePetRequest.getUrlList());
        } catch (Exception e) {
            throw new Exception("添加图片失败");
        }

        WxTemplate wxTemplate = new WxTemplate();
        wxTemplate.setId(StringUtils.getId());
        wxTemplate.setFormId(addGivePetRequest.getFormId());
        wxTemplate.setUserId(addGivePetRequest.getUserId());
        //设置过期时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, 6);
        wxTemplate.setExpiryDate(calendar.getTime());

        try {
            wxTemplateMapper.insert(wxTemplate);
        } catch (Exception e) {
            throw new Exception("添加模板失败");
        }
        //发送消息
        String type = "9";
        messageService.getMessage(addGivePetRequest.getUserId(), type);
    }


    /*
     * @author 顶风少年
     * @date 2019/6/25 11:27
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param city
     * @param time
     * @result java.util.List<com.tatang.pet.vo.getGivePetListByCityResponse>
     * @notify 根据城市获取送养列表
     * version 1.0
     */
    public List<GetGivePetListByCityResponse> getGivePetListByCity(String city, Date time) throws Exception {
        try {
            return givePetMapperExtend.getGivePetListByCity(city, time);
        } catch (Exception e) {
            throw new Exception("获取送养列表失败");
        }
    }


    /*
     * @author 顶风少年
     * @date 2019/6/26 11:10
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result java.util.List<com.datang.pet.pojo.City>
     * @notify 获取城市列表
     * version 1.0
     */
    public List<City> getCityList() throws Exception {
        try {
            CityExample cityExample = new CityExample();
            List<City> cityList = cityMapper.selectByExample(cityExample);
            redisTool.set("citylist", cityList);
            return cityList;
        } catch (Exception e) {
            throw new Exception("获取送养城市失败");
        }

    }


    /*
     * @author 顶风少年
     * @date 2019/6/27 16:50
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param id
     * @result com.datang.pet.vo.GetGiveInfoResponse
     * @notify 获取送养详情
     * version 1.0
     */
    public GetGiveInfoResponse getGiveInfo(String id) throws Exception {
        try {
            return givePetMapperExtend.getGiveInfo(id);
        } catch (Exception e) {
            throw new Exception("查询送养信息失败");
        }

    }


    /*
     * @author 顶风少年
     * @date 2019/6/30 22:28
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param userId
     * @result java.util.List<com.datang.pet.vo.GetGivePetListByCityResponse>
     * @notify 获取用户发布送养信息
     * version 1.0
     */
    public List<GetGivePetListByCityResponse> getGiveListByUserId(String userId) throws Exception {
        try {
            return givePetMapperExtend.getGiveListByUserId(userId);
        } catch (Exception e) {
            throw new Exception("获取用户送养信息失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteGiveById(String userId, String id) throws Exception {
        GivePetExample givePetExample = new GivePetExample();
        GivePetExample.Criteria criteria = givePetExample.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andUserIdEqualTo(userId);
        //获取送养
        List<GivePet> givePets = null;
        try {
            givePets = givePetMapper.selectByExample(givePetExample);
        } catch (Exception e) {
            throw new Exception("删除失败,查询送养记录失败");
        }

        //删除送养
        try {
            givePetMapper.deleteByExample(givePetExample);
        } catch (Exception e) {
            throw new Exception("删除失败,删除送养记录失败");
        }

        //拿到资源id
        String resource = givePets.get(0).getResource();

        ResourceExample resourceExample = new ResourceExample();
        ResourceExample.Criteria criteria1 = resourceExample.createCriteria();
        ResourceExample.Criteria criteria2 = criteria1.andIdEqualTo(resource);
        //查询所有资源
        List<Resource> resources = null;
        try {
            resources = resourceMapper.selectByExample(resourceExample);
        } catch (Exception e) {
            throw new Exception("删除失败,查询资源失败");
        }


        List<String> srcs = new ArrayList<>();
        for (Resource resource1 : resources) {
            srcs.add(resource1.getSrc());
        }
        ResourceExample resourceExample1 = new ResourceExample();
        ResourceExample.Criteria criteria3 = resourceExample1.createCriteria();
        criteria3.andSrcIn(srcs);
        try {
            resourceMapper.deleteByExample(resourceExample1);
        } catch (Exception e) {
            throw new Exception("删除失败,删除资源失败");
        }

        deleteFile.deleteMultiFile(srcs, 4);
        redisTool.remove("giveListByUserId" + userId);
    }
}
